package spring.angularjs.customannotation;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import spring.angularjs.controller.HomeController;
import spring.angularjs.domain.Userinfo;
import spring.angularjs.services.Services;

public class UniqueLoginIdValidator implements
		ConstraintValidator<UniqueLoginId, String> {

	@Autowired
	private Services services;

	@Override
	public void initialize(UniqueLoginId constraintAnnotation) {
		// TODO Auto-generated method stub
		SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
	}

	@Override
	public boolean isValid(String loginId, ConstraintValidatorContext context) {
		if (HomeController.page.equals("create")) {
            if (services.checkLoginId(loginId)) {
                    return false;
            } else {
                    return true;
            }
	    } 
	    
	    if (HomeController.page.equals("update") || HomeController.page.equals("profile") || HomeController.page.equals("changestatus")) {
	            if (services.checkLoginId(loginId)) {
	                    return true;
	            } else {
	                    return false;
	            }
	    }
    return false;
		
	}

}
